
package com.googlecode.mvndroid;

import com.googlecode.mvndroid.tools.ApkbuilderTool;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;


/**
 * @goal apkbuilder
 * @description Package the application.
 *
 * @author <a href="mailto:1.024e3@gmail.com">Bartosz Duzy</a>
 */

public class Apkbuilder_Mojo extends AndroidMojo{

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {

        ProcessBuilder processBuilder = new ProcessBuilder();
        ApkbuilderTool tool = new ApkbuilderTool(this);
        List<String> parameters = new ArrayList<String>();
        
        if(isGenerateUnsignedPackage()){
            parameters.add(getOutUnsignedPackage().getAbsolutePath());
            parameters.add("-u");
            getLog().info("Generating unsigned "+getOutUnsignedPackage().getName()+" package...");
            getProject().getArtifact().setFile(getOutUnsignedPackage());
        }else{
            parameters.add(getOutDebugPackage().getAbsolutePath());
            getLog().info("Generating signed debug "+getOutDebugPackage().getName()+" package...");
            getProject().getArtifact().setFile(getOutDebugPackage());
        }
        parameters.add("-z");
        parameters.add(getResourcesPackage().getAbsolutePath());
        parameters.add("-f");
        parameters.add(getDexFile().getAbsolutePath());
        parameters.add("-rf");
        parameters.add(getSrcDir().getAbsolutePath());
        if(getExternalLibsDir().exists()){
            parameters.add("-rj");
            parameters.add(getExternalLibsDir().getAbsolutePath());
        }
        if(getNativeLibsDir().exists()){
            parameters.add("-nf");
            parameters.add(getNativeLibsDir().getAbsolutePath());
        }
        tool.setParameters(parameters);
        tool.execute(processBuilder);
     
    }

}
